package exercise11;

public class Sort {

    public void quickSort (int[] array) {
        quick(array,0,array.length-1);
    }

    public void heapSort (int[] array) {
        createBigHeap(array);
        int end = array.length-1;
        while (end >= 0) {
            swap(array,0,end);
            end--;
            siftDown(array,0,end);
        }
    }

    public void mergeSort(int[] array) {
        mergeFun(array,0,array.length-1);
    }

    public void shallSort (int[] array) {
        int gap = array.length / 2;
        while (gap > 0) {
            shall(array,gap);
            gap /= 2;
        }
    }

    public void insertSort(int[] array) {
        for (int i = 1; i < array.length; i++) {
            int j = i - 1;
            int tmp = array[i];
            for (; j >= 0; j--) {
                if (array[j] >= tmp) {
                    array[j+1] = array[j];
                } else {
                    break;
                }
            }
            array[j+1] = tmp;
        }
    }

    public void bubbleSort(int[] array) {
        for (int i = 0; i < array.length; i++) {
            int flag = 0;
            for (int j = 1; j < array.length-i; j++) {
                if (array[j - 1] > array[j]) {
                    swap(array,j-1,j);
                    flag = 1;
                }
            }
            if (flag == 0) {
                break;
            }
        }
    }
    // 1 2 3 4 5 6 7 8 9
    private void mergeFun(int[] array, int start, int end) {
        if (start >= end) {
            return;
        }
        int midIndex = start + (end - start) / 2;

        mergeFun(array,start,midIndex);
        mergeFun(array,midIndex+1,end);

        // merge
        merge(array, start, end, midIndex);
    }

    private void merge(int[] array, int start, int end, int midIndex) {
        int rs = midIndex + 1 , k = 0, tmpIndex = start;
        int[] tmpArray = new int[end - start +1];
        while (start <= midIndex && rs <= end) {
            if (array[start] < array[rs]) {
                tmpArray[k++] = array[start++];
            } else {
                tmpArray[k++] = array[rs++];
            }
        }
        while (start <= midIndex) {
            tmpArray[k++] = array[start++];
        }
        while (rs <= end) {
            tmpArray[k++] = array[rs++];
        }
        for (int j = tmpIndex; j <= end; j++) {
            array[j] = tmpArray[j - tmpIndex];
        }
    }

    private void shall(int[] array, int gap) {
        for (int i = gap; i < array.length; i++) {
            int tmp = array[i];
            int j = i - gap;
            for (; j >= 0; j -= gap) {
                if (array[j] >= tmp) {
                    array[j + gap] = array[j];
                } else {
                    break;
                }
            }
            array[j+gap] = tmp;
        }
    }

    private void createBigHeap(int[] array) {
        for (int parent = (array.length-2); parent >= 0; parent--) {
            siftDown(array,parent,array.length-1);
        }
    }

    private void siftDown(int[] array, int parent, int end) {
        int child = parent * 2 + 1;
        while (child <= end) {
            if (child + 1 <= end && array[child+1] > array[child]) {
                child++;
            }
            if (array[child] > array[parent]) {
                swap(array,child,parent);
                parent = child;
                child = child * 2 + 1;
            } else {
                break;
            }
        }
    }


    private void quick(int[] array, int start, int end) {
        if (start >= end) {
            return;
        }
        // 如果最后剩余的排序个数小于等于15个，则使用直接插入排序
        if (end - start + 1 <= 15) {
            insertWithQuick(array,start,end);
            return;
        }

        // 三数取中进行优化
        int midIndex = findMidIndex(array,start,end);
        swap(array,start,midIndex);

        int part = partition2(array,start,end);

        quick(array,start,part-1);
        quick(array,part+1,end);
    }

    private int findMidIndex(int[] array, int start, int end) {
        int midIndex = start + (end - start) / 2;
        if (array[start] > array[end]) {
            // end  start
            if (array[midIndex] < array[end]) {
                return end;
            } else if (array[midIndex] > array[start]) {
                return start;
            } else {
                return midIndex;
            }
        } else {
            // start  end
            if (array[midIndex] < array[start]) {
                return start;
            } else if (array[midIndex] > array[end]) {
                return end;
            } else {
                return midIndex;
            }
        }
    }

    private void insertWithQuick(int[] array, int start, int end) {
        for (int i = start; i <= end; i++) {
            int j = i - 1;
            int tmp = array[i];
            for (; j >= start; j--) {
                if (array[j] >= tmp) {
                    array[j+1] = array[j];
                } else {
                    break;
                }
            }
            array[j+1] = tmp;
        }
    }

    private int partition2(int[] array, int left ,int right) {
        int tmp = array[left];
        while (left < right) {
            while (left < right && array[right] >= tmp) {
                right--;
            }
            array[left] = array[right];
            while (left < right && array[left] <= tmp) {
                left++;
            }
            array[right] = array[left];
        }
        array[left] = tmp;
        return left;
    }

    private int partition(int[] array, int left, int right) {
        int tmp = array[left];
        int tmpIndex = left;
        while (left < right) {
            while (left < right && array[right] >= tmp) {
                right--;
            }
            while (left < right && array[left] <= tmp) {
                left++;
            }
            swap(array,left,right);
        }
        swap(array,tmpIndex,left);
        return left;
    }

    private void swap(int[] array, int x, int y) {
        int tmp = array[x];
        array[x] = array[y];
        array[y] = tmp;
    }
}
